<?php

class FightDAO extends DAO {

    public static function getuseratk($uid = 0, $tuid = 0)
	{
        $uid = intval($uid);
        $tuid = intval($tuid);
        if (!$uid || !$tuid) return false;

        $dbconf = self::dbhash($uid, "pvn_global", "useratk");
        $dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;
        
        $sql = "select * from {$dbconf[4]} where uid={$uid} and tuid={$tuid}";
        $ret = self::dbquery($dbconn, $sql, "row");
        self::dbclose($dbconn);

		if($ret["lucks"]) $ret["lucks"] = json_decode($ret["lucks"]);

        return $ret;
    }

	public static function getuseratklist($uid = 0)
	{
        $uid = intval($uid);
        if (!$uid) return false;

        $dbconf = self::dbhash($uid, "pvn_global", "useratk");
        $dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;
        
		$dt = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
        $sql = "select * from {$dbconf[4]} where uid={$uid} and tm>={$dt}";
		$res = self::dbquery($dbconn, $sql);
		$ret = array();
        if ($res && mysql_num_rows($res) >= 1)
		{
            while ($row = mysql_fetch_assoc($res))
            {
				if($row["lucks"]) $row["lucks"] = json_decode($row["lucks"]);
				$ret[$row["tuid"]] = $row;
            }
            unset($row);
        }
		elseif($res) $ret = array();
		else $ret = false;
        self::dbclose($dbconn);		

        return $ret;
    }

    public static function newuseratk($uid = 0, $tuid = 0, $data = array())
	{
        $uid = intval($uid);
		$tuid = intval($tuid);
        if (!$uid || !$tuid || !$data || !is_array($data)) return false;

		$sets = array();
		foreach($data as $k => $v)
		{
			switch($k)
			{
				case 'tm':
					$v = intval($v);
					$sets[] = "{$k}='{$v}'";
					break;
				case 'lucks':
					$v = mysql_escape_string(json_encode($v));
					$sets[] = "{$k}='{$v}'";
					break;
			}
		}
		if(!$sets) return false;
		$sets[] = "uid='{$uid}'";
		$sets[] = "tuid='{$tuid}'";
		$sets = implode(",", $sets);
        
        $dbconf = self::dbhash($uid, "pvn_global", "useratk");
        $dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
		if(!$dbconn) return false;

        $ret = self::dbquery($dbconn, "insert into {$dbconf[4]} set {$sets}");
        self::dbclose($dbconn);
        
        return $ret;
    }

    public static function setuseratk($uid = 0, $tuid = 0, $data = array())
	{
        $uid = intval($uid);
        $tuid = intval($tuid);
		if (!$uid || !$tuid || !$data || !is_array($data)) return false;
    
        $sets = array();
		foreach($data as $k => $v)
		{
			switch($k)
			{
				case 'tm':
					$v = intval($v);
					$sets[] = "{$k}='{$v}'";
					break;
				case 'lucks':
					$v = mysql_escape_string(json_encode($v));
					$sets[] = "{$k}='{$v}'";
					break;
			}
		}
		if(!$sets) return false;
		$sets = implode(",", $sets);
        
		$dbconf = self::dbhash($uid, "pvn_global", "useratk");
		$dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
		if (!$dbconn) return false;
		
		$sql = "update {$dbconf[4]} set {$sets} where uid={$uid} and tuid={$tuid}";
		$ret = self::dbquery($dbconn, $sql);
		self::dbclose($dbconn);
		
		return $ret;
    }

}
?>